package com.jic.member.controller.manage;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.Feedback;
import com.jic.member.request.FeedbackRequest;
import com.jic.member.response.FeedbackResponse;
import com.jic.member.service.FeedbackService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: zxl
 * @date: 2020-01-09
 * @remark: BBC接口 意见反馈
 */
@RestController
@RequestMapping("/feedback")
@Slf4j
public class FeedBackController  {


    @Autowired
    RedisClientTemplate redisClientTemplate;

    @Autowired
    FeedbackService feedbackService;

    /**
     * 分页查询
     * @param form
     * @return
     */
    @ApiOperation(value = "获取所有意见反馈列表")
    @ResponseBody
    @RequestMapping("/queryList")
    public RestResult<PageResult<FeedbackResponse>> queryList(@RequestBody FeedbackRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        Feedback feedback = BeanUtil.copy(form, Feedback.class);
        PageResult<Feedback> result = feedbackService.queryPage(feedback, page);
        List<FeedbackResponse> FeedbackResponseList = result.getRows().stream().map(temp -> {
            FeedbackResponse memberAddressResponse = BeanUtil.copy(temp, FeedbackResponse.class);
            return memberAddressResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<FeedbackResponse> pageResult = new PageResult<>();
        pageResult.setRows(FeedbackResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }


    /*
     * 新增/修改意见反馈
     * */
    @ApiOperation(value = "新增/修改意见反馈")
    @ResponseBody
    @RequestMapping("/editFeedBackInfo")
    public RestResult<String> editFeedBackInfo(@RequestBody FeedbackRequest form) {
        log.info("开始进入新增/修改意见反馈=========》,{}", form.toString());
        RestResult restResult = new RestResult<>();
        try {
            Feedback feedback = BeanUtil.copy(form, Feedback.class);
            long flag=feedbackService.insert(feedback);
            restResult.setCode("0000");
        } catch (Exception e) {
            log.error("error",e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }

    /*
     * 删除意见反馈
     * */
    @ApiOperation(value = "删除意见反馈")
    @ResponseBody
    @RequestMapping("/deleteInfo")
    public RestResult<String> deleteInfo(@RequestBody FeedbackRequest form) {
        log.info("开始进入删除意见反馈=========》,{}", form);
        RestResult restResult = new RestResult<>();
        try {
            String[] checked = form.getIds().split(",");
            for(int i=0;i<checked.length;i++){
                feedbackService.delete(Long.valueOf(checked[i]));
            }
            restResult.setCode("0000");
        } catch (Exception e) {
            log.error("error",e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }
}
